/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.memory.mdo;

import org.vnetcon.blobdb.database.SQLTransactionException;


public class TableData {

	boolean isWriteProtected = false;
	boolean isInTransaction = false;
	
    private String tableDataKey;
    private int tableDataID;
    private int tableID;
    private int rowCount;
    private int freeRows;
    private int lastRowid;
    private byte[] dataRows;
    private byte[] commitRows = null;
    private long touch;

    
    public TableData(int tableDataID, int tableID){
    	this.tableDataID = tableDataID;
    	this.tableID = tableID;
    	PMF.hTableData.put(tableDataID, this);
    }
    
    public void setTableDataKey(String tableDataKey){
    	this.tableDataKey = tableDataKey;
    }
    
	public int getTableID() {
		return tableID;
	}

	public byte[] getDataRows(boolean forWrite) throws SQLTransactionException {
		if(this.isWriteProtected && forWrite){
			throw new SQLTransactionException("Block is currently write protected.");
		}
		
		this.isWriteProtected = forWrite;
		return dataRows;
	}

	public void setDataRows(byte[] dataRowsParam) throws SQLTransactionException {
		if(!this.isWriteProtected){
			throw new SQLTransactionException("Can't update block without write protection.");
		}
		this.commitRows = dataRowsParam;
	}
	

	public String getTableDataKey() {
		return tableDataKey;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getFreeRows() {
		return freeRows;
	}

	public void setFreeRows(int freeRows) {
		this.freeRows = freeRows;
	}

	public int getLastRowid() {
		return lastRowid;
	}

	public void setLastRowid(int lastRowid) {
		this.lastRowid = lastRowid;
	}

	public int getTableDataID() {
		return tableDataID;
	}

	public void touch(long l){
		this.touch = l;
	}

	public void save() {
		if(this.commitRows != null){
			this.dataRows = this.commitRows;
		}
		this.commitRows = null;
		this.isWriteProtected = false;
		this.isInTransaction = false;
	}

	public void rollback(){
		this.commitRows = null;
		this.isWriteProtected = false;
		this.isInTransaction = false;
	}
	
	public boolean isInTransaction() {
		return isInTransaction;
	}

	public void setInTransaction(boolean isInTransaction) {
		this.isInTransaction = isInTransaction;
	}
	
	
}
